#####################################################################################################
############################## Appendix (A) #########################################################
#####################################################################################################

#Load packages
library(stargazer)

#Loading the main dataset: 
# data_2rounds: Contains data on the 2 rounds of the election, excluding the pre-electoral period 
# data_3rounds: Contains data on the 2 rounds of the election, including the pre-electoral period as an additional round

data <- read_dta("data_2rounds.dta")
data3 <- read_dta("data_3rounds.dta")


#Create dictionary to present the names of the variables in the tables
dict=c("mb_dummy"="MB Running", "pct_urban"="Urban (%)", "pct_emp"="Employment (%)", 
       "sdeduscore"="Education (sd)","eduscore2"="Education2 (sd)", "gov_employed"="Gov. Employment (%)", "logprotest"="Protest (log)", 
       "highprofile"="High Profile", "logregistered"="Registered (log)", "female_pct"="Female (%)", "round2"="Runoff", 
       "votebuyingd"="Vote-buying", "fmean.female_pct"="Female", "fmean.logregistered"="Registered (log)", 
       "fmean.pct_urban"="Urban (%)", "fmean.pct_emp"="Employment (%)", "fmean.sdeduscore"="Education (sd)", 
       "fmean.gov_employed"="Gov. Employment (%)", "newndp"="New NDP", "split_ndp"="NDP Dissidents", 
       "incumbent"="Incumbents", "nocand_competing"="Candidates No.", "death_ndpaffil"="NDP Death", "client_index"="Clientelism", 
       "watani_1984" = "NDP 1984 (%)", "wafd_1984" = "MB Coalition 1984 (%)", "fmean.incumbent"="Incumbents", "fmean.nocand_competing"="Candidates No.", 
       "fmean.newndp"="New NDP", "fmean.split_ndp"="NDP Dissidents", "fmean.logprotest"="Protest (log)")


####### Table (2)
subdata <- subset(data, select=c("allintimid", "voter_total", "state_intimidation", "nonstate_intimidation", "mb_dummy", 
                                 "newndp", "split_ndp", "pct_urban", "pct_emp", "sdeduscore", "logprotest", "logregistered",
                                 "female_pct", "incumbent", "nocand_competing", "turnout_pct", "watani_1984", 
                                 "wafd_1984"))

stargazer(as.data.frame(subdata), type="latex", title="Descriptive Statistics of the Main Variables", summary=TRUE, 
          median=T, iqr = FALSE, dict="dict")



####### Table (3)
subdata <- subset(data3, select=c("allintimid", "voter_total", "state_intimidation", "nonstate_intimidation", "mb_dummy", 
                                  "newndp", "split_ndp", "pct_urban", "pct_emp", "sdeduscore", "logprotest", "logregistered",
                                  "female_pct", "incumbent", "nocand_competing", "turnout_pct", "watani_1984", 
                                  "wafd_1984"))


stargazer(as.data.frame(subdata), type="latex", title="Descriptive Statistics of the Main Variables", summary=TRUE, 
          median=T, iqr = FALSE)



####### Table (4)
subdata <- subset(subset(data, data$round==1), select=c("allintimid", "voter_total", "state_intimidation", "nonstate_intimidation", "mb_dummy", 
                                                        "newndp", "split_ndp", "pct_urban", "pct_emp", "sdeduscore", "logprotest", "logregistered",
                                                        "female_pct", "incumbent", "nocand_competing",  "turnout_pct"))


stargazer(as.data.frame(subdata), type="latex", title="Descriptive Statistics of the Main Variables", summary=TRUE, 
          median=T, iqr = FALSE)
